package com.xbts.system.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.xbts.common.annotation.Excel;
import com.xbts.common.core.domain.BaseEntity;
import com.xbts.common.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 素材对象 sei_material
 *
 * @author xbts
 * @date 2023-08-24
 */
@Data
@ApiModel("素材对象")
@TableName("sei_material")
public class SeiMaterial extends BaseEntity {

    private static final long serialVersionUID = 1L;

    /**
     * 素材id
     */
    @TableId
    private String materialId;

    /**
     * 素材名称
     */
    @Excel(name = "素材名称")
    @ApiModelProperty("素材名称")
    private String name;


    /**
     * 父级名称
     */
    @Excel(name = "父级名称")
    @ApiModelProperty("父级名称")
    @TableField(exist = false)
    private String parentName;

    /**
     * 素材内容
     */
    @Excel(name = "素材内容")
    @ApiModelProperty("素材内容")
    private String content;

    /**
     * 父级id
     */
    @Excel(name = "父级id")
    @ApiModelProperty("父级id")
    private String parentId;

    /**
     * 祖父级id
     */
    @Excel(name = "祖父级id")
    @ApiModelProperty("祖父级id")
    private String grandparent;

    @ApiModelProperty("一级类型名称")
    @TableField(exist = false)
    private String grandparentName;

    /**
     * 素材隶属
     */
    @Excel(name = "素材隶属")
    @ApiModelProperty("素材隶属")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long materialAffiliation;

    /**
     * 素材人员
     */
    @Excel(name = "素材人员")
    @ApiModelProperty("素材人员")
    @TableField(exist = false)
    private String affiliationName;

    /**
     * 祖先
     */
    @Excel(name = "祖先")
    @ApiModelProperty("祖先")
    private String ancestors;


    @ApiModelProperty("类型(1一级类型，2二级类型，3素材)")
    private Integer type;

    /**
     * 子集
     */
    @Excel(name = "子集")
    @ApiModelProperty("子集")
    @TableField(exist = false)
    private List<SeiMaterial> children = new ArrayList<>();


    /**
     * 是否存在子集
     */
    @Excel(name = "子集")
    @ApiModelProperty("子集")
    @TableField(exist = false)
    private Boolean hasChildren;

    @ApiModelProperty("模板选择的素材ID")
    @TableField(exist = false)
    private String selectedMaterialId;

    @ApiModelProperty("模板选择的素材名称")
    @TableField(exist = false)
    private String selectedMaterialName;

    public <T> QueryWrapper<T> queryWrapper() {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(materialId), "m.material_id", materialId);
        queryWrapper.like(StringUtils.isNotEmpty(name), "m.name", name);
        queryWrapper.like(StringUtils.isNotEmpty(parentId), "m.parent_id", parentId);
        queryWrapper.eq(StringUtils.isNotNull(type), "m.type", type);
        queryWrapper.eq(StringUtils.isNotNull(materialAffiliation), "m.material_affiliation", materialAffiliation);
        queryWrapper.eq(StringUtils.isNotEmpty(grandparent), "m.grandparent", grandparent);
        queryWrapper.orderByDesc("m.create_time");
        return queryWrapper;
    }
}